﻿var Drawline = null;
var stapop = void (0);
var staicon = void (0);
var endpop = void (0);
var endicon = void (0);
var startpoint = void (0);
var endpoint = void (0);

function userLocusInitial()//仅仅显示用户轨迹的页面初始化函数
{
    if (dMap == null)
        dMap = new DMap(document.getElementById("mapcontainer"));
    var icenter = new DLonLat(118.5, 36.5);
    var izoom = 12;
    dMap.setCenter(icenter, izoom);
    dMap.enableScrollWheelZoom();
    dMap.addLayer(D_OVERLAY_TDT_VECTOR_LAYER);
    dMap.addLayer(D_OVERLAY_VECTOR_LAYER);
    dMap.addLayer(D_OVERLAY_DY_RASTER_LAYER);
    D_OVERLAY_DY_RASTER_LAYER.display(false);
    dMap.addLayer(D_OVERLAY_TDT_RASTER_LAYER);
    D_OVERLAY_TDT_RASTER_LAYER.display(false);
    dMap.addLayer(D_OVERLAY_DY_VECTOR_LAYER);
    showMapCenterPosition();
    showScale();
    showLevel();
    showEagleEye();
    setKeyEnable();
    dMap.addListener("mousemove", showMousePosition, this);
    dMap.addListener("dragend", showMapCenterPosition, this);
}





//根据多个用户ID，以及起始时间段搜索符合条件的轨迹组
function SearchLocusGroup() {
    var strlist = document.getElementById("ListBoxUsers"); //获取Listbox
    var pattern = /^\d{4}\-\d{1,2}\-\d{1,2}$/;
    var startTime = document.getElementById("txtStartTime").value;
    var endTime = document.getElementById("txtEndTime").value;
    var flag1 = pattern.test(startTime);
    if (!flag1) {
        alert("起始日期格式不正确！");
        return;
    }
    var flag2 = pattern.test(endTime);
    if (!flag2) {
        alert("结束日期格式不正确！");
        return;
    }
    var str = "";
    if (strlist.options.length > 0 && startTime != '' && endTime != '') {
        for (var i = 0; i < strlist.options.length; i++) {
            if (strlist.options[i].selected == true) {
                var j = strlist.options[i].value;
                str += j + ";"; //把Value值串起来
            }
        }
        if (str != '' && str != '0000') {
            var strValue = str.replace(/;$/, "");   //去掉最后一个逗号
            CreateListWindowInfo(strValue, startTime, endTime);
        }
        else {
            alert("请选择至少一个实时用户！");
        }
    }
}
//根据用户的ID，返回用户的轨迹组列表
function CreateListWindowInfo(listBoxUsersStr, startTimeStr, endTimeStr) {

    var xmlURL = "GetUserLocusGroup.aspx?id=" + listBoxUsersStr + "&Start=" + startTimeStr + "&End=" + endTimeStr;
    if (window.ActiveXObject) {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else if (window.XMLHttpRequest) {
        xmlHttp = new XMLHttpRequest();
    }
    xmlHttp.onreadystatechange = handle;
    xmlHttp.open("GET", xmlURL, true);
    xmlHttp.send(null);

    //轨迹起点点击显示弹出文本
    dMap.addListener("click", function(point) {
        try {
            var B1 = startpoint.lat;
            var B2 = startpoint.lat + 0.15;
            var L1 = startpoint.lon - 0.05;
            var L2 = startpoint.lon + 0.05;
            var B = point.lat;
            var L = point.lon;
            var bool1;
            var bool2;
            bool1 = ((eval(B1) < eval(B)) && (eval(B) < eval(B2)));
            bool2 = ((eval(L1) < eval(L)) && (eval(L) < eval(L2)));
            if (bool1 && bool2) {
                stapop.show(true);
            }
        }
        catch (e) { }
    })
    //轨迹终点点击显示弹出文本
    dMap.addListener("click", function(point) {
        try {
            var B1 = endpoint.lat;
            var B2 = endpoint.lat + 0.15;
            var L1 = endpoint.lon - 0.05;
            var L2 = endpoint.lon + 0.05;
            var B = point.lat;
            var L = point.lon;
            var bool1;
            var bool2;
            bool1 = ((eval(B1) < eval(B)) && (eval(B) < eval(B2)));
            bool2 = ((eval(L1) < eval(L)) && (eval(L) < eval(L2)));
            if (bool1 && bool2) {
                endpop.show(true);
            }
        }
        catch (e) { }
    })

}

function handle() {
    var divInfos = '';
    var xml;
    if (xmlHttp.readyState == 4) {
        if (xmlHttp.status == 200) {
            xml = xmlHttp.responseXML;
            var userRecords = xml.documentElement.getElementsByTagName("Record");
            if (userRecords[0] == null) {
                alert("无轨迹组可显示");
                return false;
            }
            if (userRecords[0].getAttribute("SurveyRecID") != '0000') {
                for (var i = 0; i < userRecords.length; i++) {
                    var surveyID = userRecords[i].getAttribute("SurveyRecID");
                    var TimeSpan = userRecords[i].getAttribute("TimeSpans");

                    var OnLineTime = userRecords[i].getAttribute("StartTime");
                    var unitName = userRecords[i].getAttribute("UnitName");
                    var userName = userRecords[i].getAttribute("RTKUserName");
                    var pointNums = userRecords[i].getAttribute("PointNum");

                    // 创建“查看历史轨迹”按钮
                    var linkinnerHTML = '<br />' + OnLineTime + '<br />姓名:' + userName + '<br />所属单位:' + unitName + '<br />在线时长:' + TimeSpan + '<br />测点数:' + pointNums + '<br />';
                    var clickeventPro = 'onclick="CreateUserLocus(' + surveyID + ')"';
                    var atempHtml = '<a href="#" ' + clickeventPro + ' >' + linkinnerHTML + '</a>';
                    divInfos = divInfos + atempHtml;

                }

                document.getElementById("divlist").innerHTML = divInfos;
                showlist();
            }
            else
            //alert(userRecords[0].getAttribute("UserName"));
            {
                alert("无轨迹组可显示");
                return false;
            }
        }
    }

}

//绘制该用户特定轨迹组的轨迹
function CreateUserLocus(userLocusID) {

    dMap.removeOverlay(Drawline);
    dMap.removeOverlay(stapop);
    dMap.removeOverlay(staicon);
    dMap.removeOverlay(endpop);
    dMap.removeOverlay(endicon);
    stapop = void (0);
    staicon = void (0);
    endpop = void (0);
    endicon = void (0);
    startpoint = void (0);
    endpoint = void (0);

    var DrawxmlURL = "GetUserLocusDetail.aspx?id=" + userLocusID;

    if (window.ActiveXObject) {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else if (window.XMLHttpRequest) {
        xmlHttp = new XMLHttpRequest();
    }
    xmlHttp.onreadystatechange = Drawhandle;
    xmlHttp.open("GET", DrawxmlURL, true);
    xmlHttp.send(null);
}

function Drawhandle() {
    var Drawxml;
    var markerPoints = "";
    if (xmlHttp.readyState == 4) {
        if (xmlHttp.status == 200) {
            Drawxml = xmlHttp.responseXML;
            var userLocus = Drawxml.documentElement.getElementsByTagName("Record");
            if (userLocus[0] == null) {
                alert("轨迹组内无数据");
                return false;
            }
            //alert(userRecords.length);
            if (userLocus[0].getAttribute("YearRecID") != "0000") {
                var tempValid = true; //用于判断该轨迹里面有没有不合法测量
                var tempvalidstring = "合法用户";
                for (var i = 0; i < userLocus.length; i++) {

                    var username = userLocus[i].getAttribute("RTKUserName");
                    var unitname = userLocus[i].getAttribute("UnitName");

                    var surveyB = parseFloat(userLocus[i].getAttribute("RealB"));
                    var surveyL = parseFloat(userLocus[i].getAttribute("RealL"));
                    if (surveyB < 1 || surveyL < 1) {
                        continue;
                    }
                    //userNames.push(username);
                    var surveyH = userLocus[i].getAttribute("RealH");
                    var surveyValid = userLocus[i].getAttribute("IsWithArea").toLowerCase(); //True ,False
                    var surveyTime = userLocus[i].getAttribute("SurveyTime");
                    markerPoints += surveyL + "," + surveyB + ",";

                    if (surveyValid == "false") {
                        tempValid = false;
                        tempvalidstring = "违规用户";
                    }
                    var markerpoint = new DLonLat(parseFloat(surveyL), parseFloat(surveyB));
                    if (i == 0) {
                        startpoint = new DLonLat();
                        startpoint = markerpoint;
                        var title;
                        var content;
                        var config;

                        if (!staicon) {
                            //定义点符号
                            staicon = new DIcon(startpoint);
                        }
                        //添加地图覆盖物
                        dMap.addOverlay(staicon);

                        title = "<div style='margin-top:4px;font-size:12px;color:#000FFF;'>" + "轨迹起点实测信息：" + "</div>";
                        content = "<div style='margin-top:4px;font-size:12px;color:black;'>" + "用户名：" + username + "</div>" + "<div style='margin-top:4px;font-size:12px;color:black;'>" + "单位名：" + unitname + "</div>" + "<div style='margin-top:4px;font-size:12px;color:black;'>" + "测量时间：" + surveyTime + "</div>" + "<div style='margin-top:4px;font-size:12px;color:black;'>" + "纬度：" + surveyB + "</div>" + "<div style='margin-top:4px;font-size:12px;color:black;'>" + "经度：" + surveyL + "</div>" + "<div style='margin-top:4px;font-size:12px;color:black;'>" + "大地高：" + surveyH + "</div>" + "<div style='margin-top:4px;font-size:12px;color:black;'>" + "合法性：" + tempvalidstring + "</div>";
                        config = { color: "red", titleType: "html", flash: true, height: 140, width: 200, offsety: -40 };

                        if (!stapop) {
                            stapop = new DPopup(startpoint, title, content, config);
                            dMap.addOverlay(stapop);
                            //                            stapop.show(true);
                        }
                    }
                    if (i == userLocus.length - 1) {
                        endpoint = new DLonLat();
                        endpoint = markerpoint;
                        var title;
                        var content;
                        var config;

                        if (!endicon) {
                            //定义点符号
                            endicon = new DIcon(endpoint);
                        }
                        //添加地图覆盖物
                        dMap.addOverlay(endicon);

                        title = "<div style='margin-top:4px;font-size:12px;color:#000FFF;'>" + "轨迹终点实测信息：" + "</div>";
                        content = "<div style='margin-top:4px;font-size:12px;color:black;'>" + "用户名：" + username + "</div>" + "<div style='margin-top:4px;font-size:12px;color:black;'>" + "单位名：" + unitname + "</div>" + "<div style='margin-top:4px;font-size:12px;color:black;'>" + "测量时间：" + surveyTime + "</div>" + "<div style='margin-top:4px;font-size:12px;color:black;'>" + "纬度：" + surveyB + "</div>" + "<div style='margin-top:4px;font-size:12px;color:black;'>" + "经度：" + surveyL + "</div>" + "<div style='margin-top:4px;font-size:12px;color:black;'>" + "大地高：" + surveyH + "</div>" + "<div style='margin-top:4px;font-size:12px;color:black;'>" + "合法性：" + tempvalidstring + "</div>";
                        config = { color: "red", titleType: "html", flash: true, height: 140, width: 200, offsety: -40 };

                        if (!endpop) {
                            endpop = new DPopup(endpoint, title, content, config);
                            dMap.addOverlay(endpop);
                            //                            endpop.show(true);
                        }
                    }
                    if (i == 0) {
                        dMap.setCenter(markerpoint, 7);
                    }

                }
                markerPoints = markerPoints.substr(0, markerPoints.length - 1);

                if (tempValid == true) {
                    //                if (!Drawline & tempValid == true) {
                    Drawline = new DPolyline(markerPoints, { color: "green", weight: 3, opacity: 0.9 });
                    dMap.addOverlay(Drawline);
                }
                else {
                    Drawline = new DPolyline(markerPoints, { color: "red", weight: 3, opacity: 0.9 });
                    dMap.addOverlay(Drawline);
                }
            }
        }
    }
}


function showquery() {
    document.getElementById("divquery").style.display = "";
    document.getElementById("divlist").style.display = "none";
    //    document.getElementById("divquery").setAttribute('style','display:block;');
    //    document.getElementById("divlist").setAttribute('style','display:none;');
    return false;
}
function showlist() {
    document.getElementById("divquery").style.display = "none";
    document.getElementById("divlist").style.display = "";
    //    document.getElementById("divquery").setAttribute('style','display:none;');
    //    document.getElementById("divlist").setAttribute('style','display:block;');
    return false;
}

function btnDialog1() {
    var chBox = document.getElementById("Checkbox2");
    if (chBox.checked)
        $("#dialog").dialog('open');
    else {
        $("#dialog").dialog('close');
    }
}

function ClearLocus() {
    dMap.removeOverlay(Drawline);
    dMap.removeOverlay(stapop);
    dMap.removeOverlay(staicon);
    dMap.removeOverlay(endpop);
    dMap.removeOverlay(endicon);
}